package org.neuedu.ssmp.crm.controller;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.neuedu.ssmp.crm.entity.ServeList;

import org.neuedu.ssmp.crm.entity.ServiceManagementHandle;
import org.neuedu.ssmp.crm.entity.vo.ResponseEntity;
import org.neuedu.ssmp.crm.service.ICustomerService;
import org.neuedu.ssmp.crm.service.IServeListService;
import org.neuedu.ssmp.crm.service.IServeService;
import org.neuedu.ssmp.crm.service.IServeStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/serveList")
public class ServeListController {
    @Autowired
    private IServeListService iServeListService;
    @Autowired
    private IServeService service;
    @Autowired
    private IServeStateService stateService;
    @Autowired
    private ICustomerService customerService;

    /**
     * @return服务创建页面
     */
    @PostMapping("create")
    public ResponseEntity add(@RequestBody ServeList serveList) {
        serveList.setCreateTime(LocalDate.now());
        serveList.setState(1);
        return iServeListService.add(serveList);
    }

    @GetMapping("sysUserInfo")
    public ResponseEntity sysUserInfo() {
        return iServeListService.sysUserInfo();
    }

    @GetMapping("severStateInfo")
    public ResponseEntity severStateInfo() {
        return iServeListService.severStateInfo();
    }

    /**
     * @return服务处理和分配页面返回数据
     */
    @GetMapping("serviceHandle")
    public ResponseEntity ServeHandle(@RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "summry", required = false) String summry,
                                      @RequestParam(value = "createTime", required = false) String createTime,
                                      @RequestParam(value = "from") Integer from,
                                      @RequestParam(value = "userId", required = false) Integer userId) {
        return ResponseEntity.data(iServeListService.conditionQuery(name, summry, createTime, from, userId));
    }


    /**
     * @return服务处理和分配页面更新数据
     */
    @PutMapping("serviceUpdate")
    public ResponseEntity editAllot(@RequestBody ServiceManagementHandle serviceManagement) {
        return iServeListService.edit(serviceManagement);
    }

    @DeleteMapping("serviceAllotDel")
    public ResponseEntity delAllot(int id) {
        return iServeListService.del(id);
    }

    /**
     * @return服务归档页面返回数据
     */
    @GetMapping("/serviceArchivingPage")
    public ResponseEntity ServeArching() {
        return ResponseEntity.data(iServeListService.findAllsServiceArchiving());
    }

    /**
     * @return服务反馈返回的数据
     */
    @GetMapping("/srviceFeedback")
    public ResponseEntity SrviceFeedback() {
        return ResponseEntity.data(iServeListService.findAllsSsrviceFeedback());
    }

    /**
     * @return返回服务类型，用于下拉框
     */
    @GetMapping("/getServeType")
    public ResponseEntity GetServeType() {
        return ResponseEntity.data(service.list());
    }

    /**
     * @return返回服务状态，用于下拉框
     */
    @GetMapping("/getServeState")
    public ResponseEntity GetServeState() {
        return ResponseEntity.data(stateService.list());
    }

    /**
     * @return返回服务状态，用于下拉框
     */
    @GetMapping("/customerInfo")
    public ResponseEntity getCustomerInfo() {
        return ResponseEntity.data(customerService.list());
    }

    /**
     * @param id            id
     * @param processResult 处理结果
     * @param stars         满意度
     * @param state         状态
     * @return
     */
    @GetMapping("/updateServeList")
    public ResponseEntity UpdateRes(@RequestParam("id") int id, @RequestParam("processResult") String processResult, @RequestParam("stars") int stars, @RequestParam("state") int state) {
        final boolean flag = iServeListService.updateById(ServeList.builder().id(id).resultContent(processResult).agreeLevel(stars).state(state).build());
        if (flag) {
            return ResponseEntity.ok("保存成功");
        }
        return ResponseEntity.error("保存失败");
    }

    /**
     * @param name
     * @param summry
     * @param createTime
     * @throws ParseException
     * @return服务归档和服务反馈的搜索返回
     */
    @GetMapping("/search")
    public ResponseEntity serviceArchiveSearch(String name, String summry, String createTime, String from) throws ParseException {
        String head = "%";
        String end = "%";
        if (Objects.nonNull(name)) {
            name = head + name + end;
        }
        if (Objects.nonNull(summry)) {
            summry = head + summry + end;
        }
        if (Objects.nonNull(createTime)) {
            if (createTime.equals("1970-1-1")) {
                createTime = head + end;
            } else {
                createTime = head + createTime + end;
            }
        }
        if (from.equals("1")) {
            return ResponseEntity.data(iServeListService.serviceFeedback(name, summry, createTime));
        }
        return ResponseEntity.data(iServeListService.serviceArchive(name, summry, createTime));
    }
}
